
import java.util.ArrayList;
import java.util.Scanner;

// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class shoppingcartest {
    public static void main(String[] args) {
        ArrayList <shoppingCar>list =new ArrayList();
        loop:
        while (true) {
            System.out.println("----------------购物车管理-------------");
            System.out.println("1：添加商品到购物车");
            System.out.println("2：将商品从购物车中移除");
            System.out.println("3：修改购物车中的商品");
            System.out.println("4：查询购物车中的商品");
            System.out.println("5:退出系统");
            System.out.println("6:结账");
            System.out.println("7:查看购物历史");
            System.out.println("请输入你的选择");
            Scanner cs = new Scanner(System.in);
            String choose = cs.next();
            switch (choose) {
                case "1":
                    adduser(list);
                    break;
                case "2":
                    deletestore(list );
                    break;
                case "3":
                    updatestore(list);
                    break;
                case "4":
                    querystore(list);
                    break;
                case "5":
                    System.out.println("5:退出系统");
                    break loop;
                case "6":
                    pay(list);
                case "7":
                    Shoppinghistory(list);
                default:

                    break;
            }


        }
    }

    private static void Shoppinghistory(ArrayList<shoppingCar> list) {
        String shoppingTime="2024年8月24日";
        String shoppingSome="联想电脑";
        String shoppingmoney="5000";
        String shoppingid="yunda001";
        String type="支付成功";
        System.out.println("消费时间\t\t\t消费物品\t\t支付金额\t\t购买状态");
        System.out.println(shoppingTime+"\t"+shoppingSome+"\t\t"+shoppingmoney+"\t\t"+type);

    }

    private static void pay(ArrayList<shoppingCar> list) {
        adduser(list);
        String payways = "微信支付";
        Scanner cs = new Scanner(System.in);
        System.out.println("请选择支付方式");
        String pay = cs.next();
        if (pay.equals(payways)) {

            System.out.println("支付成功,商品库存已修改"+list.size());
        }

    }


    private static void adduser(ArrayList<shoppingCar> list) {
        Scanner cs = new Scanner(System.in);
        System.out.println("请输入加入购物车商品的编号");
        String id = cs.next();
        System.out.println("请输入加入购物车商品的名称");
        String name = cs.next();
        System.out.println("请输入加入购物车商品的数量");
        String number = cs.next();
        System.out.println("请输入加入购物车商品的价格");
        int price = cs.nextInt();

        shoppingCar s =new shoppingCar(id,number,price,name);
        list.add(s);
        System.out.println("购物车商品添加成功");
    }

    private static void querystore(ArrayList shoppingCar) {
        if (shoppingCar.size()== 0) {
            System.out.println("当前无商品在购物车");
            return;
        }
        //打印表头信息
        System.out.println("id\t\t\t商品名\t数量\t\t价格");

        for (int i = 0; i < shoppingCar.size(); i++) {
            shoppingCar stu = (shoppingCar) shoppingCar.get(i);
            System.out.println(stu.getId()+"\t"+ stu.getType()+"\t\t"+stu.getNumber()+"\t"+stu.getPrice());
        }
    }

    private static void updatestore(ArrayList shoppingCar) {
        Scanner cs = new Scanner(System.in);
        System.out.println("请输入在购物车中要修改的商品信息的id:");
        String id = cs.next();
        int index = 0;
        getIndex(shoppingCar, id);
        if (index == -1) {
            System.out.println("要修改的" + id + "不存在，请重新输入");
            return;
        }
        //获取要修改的商品对象

        shoppingCar stu = (shoppingCar) shoppingCar.get(index);
        //输入其他信息并修改
        System.out.println("请输入在购物车中要修改的商品数量");
        String newname = cs.next();
        System.out.println("请输入在购物车中商品预支付金额");
        String pay = cs.next();
        stu.setNumber(newname);
        System.out.println("修改信息成功");
    }

    private static void deletestore(ArrayList shoppingCar) {
        Scanner cs = new Scanner(System.in);
        System.out.println("请输入要清除购物车的商品id");
        String id = cs.next();
        int index = getIndex(shoppingCar, id);
        //对index进行判断
        if (index >= 0) {
            shoppingCar.remove(index);
            System.out.println("id为：" + id + "的商品信息已经在购物车中删除");
        } else {
            System.out.println("id不存在，删除失败");
        }



    }
    public static boolean contions(ArrayList shoppingCar, String id) {
        //循环遍历集合
        //     for (int i = 0; i < list.size(); i++) {
        // Admin.Store stu = list.get(i);
        //  String sid = stu.getId();
        //   if (stu.getId().equals(id)) {
        //     return true;
        //     }
        //   }
        //  return false;
        return getIndex(shoppingCar,id)>=0;
    }

    //通过id获取索引



    //通过id获取索引
    public static int getIndex(ArrayList shoppingCar, String id) {
        for (int i = 0; i < shoppingCar.size(); i++) {

            shoppingCar stu = (shoppingCar) shoppingCar.get(i);

            if (stu.getId().equals(id)) {
                return i ;


            }
        }
        //当不存在时返回
        return-1;
    }









}